<?php defined('SYSPATH') or die('No direct access allowed.');

// 会员通用增强
// member_linkman<常用联系人> | member_invoice<会员发票> | member_address<会员地址>

class Model_Api_V2_Member_Boost
{

    private $member_id;

    private $table;


    protected $fields;

    private $primary_key = 'id';

    public function __construct($table, $member_id)
    {
        $this->table = $table;
        $this->member_id = (int) $member_id;
    }

    //设置member_id
    public function set_member_id($meber_id)
    {
        $this->member_id = $meber_id;
    }

    //设置表
    public function set_table($table)
    {
        $this->table = $table;
    }

    //设置字段
    public function set_fields($field)
    {
        $this->fields = is_string($field) ? explode(',', $field) : $field;
    }

    //列表
    public function list_item($param, $fn = null)
    {
        $obj = DB::select()->from($this->table)->where('memberid', '=', $this->member_id);
        if ($fn && is_callable($fn)) {
            $obj = $fn($obj);
        }
        $totalObj = clone $obj;
        if (!empty($param->page) && !empty($param->pagesize)) {
            $obj->offset(($param->page - 1) * $param->pagesize);
        }
        if (!empty($param->pagesize)) {
            $obj->limit($param->pagesize);
        }
        $obj->order_by('id', 'desc');
        return array('total' => $totalObj->select(array(DB::expr('COUNT( `id`)'), 'total'))->as_object()->execute()->current()->total, 'list' => $obj->execute()->as_array());
    }

    //详情
    public function info_by_id($id)
    {
        $info = DB::select()->from($this->table)->where('memberid', '=', $this->member_id)->where('id', '=', $id)->execute()->current();
        return !empty($info) ? $info : (object)array();
    }

    //删除
    public function delete($id)
    {
        if (is_string($id) || is_integer($id)) {
            $id = array($id);
        }
        if (!is_array($id)) {
            throw new TypeError('parameter type error');
        }
        $rows = DB::delete($this->table)->where('memberid', '=', $this->member_id)->where('id', 'in', $id)->execute();
        return $rows ? true : false;
    }

    //插入
    public function insert($data)
    {
        $data->memberid = $this->member_id;
        $data = (array) $data;
        foreach ($data as $k => $v) {
            if (!in_array($k, $this->fields)) {
                unset($data[$k]);
            }
        }
        list($insert_id, $rows) = DB::insert($this->table, array_keys($data))->values(array_values($data))->execute();
        if ($insert_id && !empty($data['is_default'])) {
            DB::update($this->table)->set(array('is_default' => 0))->where($this->primary_key, '!=', $insert_id)->execute();
        }
        return $rows > 0 ? $insert_id : 0;
    }

    //更新
    public function update($data)
    {
        $data->memberid = $this->member_id;
        $data = (array) $data;
        foreach ($data as $k => $v) {
            if (!in_array($k, $this->fields)) {
                unset($data[$k]);
            }
        }
        $primary_key_value = $data[$this->primary_key];
        unset($data[$this->primary_key]);
        //检测是否有更改值
        $query = DB::select()->from($this->table)->where($this->primary_key, '=', $primary_key_value);
        foreach($data as $key=>$value) {
            $query->and_where($key,'=',$value);
        }
        $info = $query->execute()->current();
        if(!isset($info['id'])) {
            $rows = DB::update($this->table)->set($data)->where($this->primary_key, '=', $primary_key_value)->execute();
            if (!empty($data['is_default'])) {
                DB::update($this->table)->set(array('is_default' => 0))->where($this->primary_key, '!=', $primary_key_value)->execute();
            }
            return $rows > 0 ? array('status'=>true) : array('status'=>false,'msg'=>'修改失败');
        }else{
            return array('status'=>false,'msg'=>'修改失败,未更改任何值');
        }



    }
}
